# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core import urlresolvers
from django.contrib.auth.models import User

from game.models import Ant

class Player(models.Model):

    CH_TYPE_BONUS = (
        ('A', _(u'Attack')),
        ('D', _(u'Defense')),
        ('S', _(u'Speed')),
        ('P', _(u'Production')),
    )

    session_id = models.CharField(max_length=20, verbose_name="Session Id", blank=True, null=True)
    
    delay = models.FloatField(verbose_name=_(u'Player delay'), blank=True, null=True, default=0.0)
    
    #team = models.ForeignKey('game.Team', verbose_name="Team", blank=True, null=True, related_name='players')
    #user_profile = models.ForeignKey(User, verbose_name=_(u'User Profile'), blank=True, null=True, related_name='players')

    name = models.CharField(verbose_name=_(u'Player name'), max_length=200, blank=False, null=False)
    game = models.ForeignKey('game.Game', verbose_name="Game", blank=False, null=False, related_name='players')
    is_dead = models.BooleanField(verbose_name=_(u'Is dead?'), blank=False, null=False, default=False)
    kill_points = models.PositiveIntegerField(verbose_name=_(u'Number of dead ants'), blank=False, null=False, default=0)
    bonus = models.CharField(verbose_name=_(u'Bonus'), max_length=4, blank=False, null=False, choices=CH_TYPE_BONUS)
    date_created = models.DateTimeField(null=True, auto_now_add=True)
    date_updated = models.DateTimeField(null=True, auto_now=True)

    class Meta:
        app_label = 'game'
        verbose_name = _(u'Player')
        verbose_name_plural = _(u'Players')

    def __unicode__(self):
        return unicode(self.name)

    # public
    def is_player_over(self):
        if self.bases.count() == 0:
            return True
        else:
            return False

    # public
    def player_over(self):
        """
        statistic = self.user_profile.statistics.get(status = 'P')
        statistic.num_deads += self.kill_points
        statistic.games += 1
        statistic.games_lose += 1
        """
        self.is_dead = True
        self.save()
        # self.team.team_over()

    # public
    def get_growth_statistics(self):

        count = 0.0
        count += (self.ants_player.filter(type_ant = 'W').count() * 1)
        count += (self.ants_player.filter(type_ant = 'OO').count() * 2)
        count += (self.ants_player.filter(type_ant = 'OD').count() * 2)
        count += (self.ants_player.filter(type_ant = 'FO').count() * 4)
        count += (self.ants_player.filter(type_ant = 'FD').count() * 4)
        count += (self.ants_player.filter(type_ant = 'Q').count() * 20)
        # añadir puntos por cantidad de hormigueros conquistados.
        # count += 50 * self.bases.count()
        """
        for base in self.bases.all():
            count += 20 * base.level
            if base.is_special:
                count += 20
        """
        return count

    def save(self, *args, **kwargs):
        if not self.pk:
            num_players = 2
            if self.game.players.all().count() > num_players:
                raise Exception(u'No se permiten más de ' + unicode(num_players) + u' jugadores en la partida.')
            elif self.game.players.filter(name = self.name).exists():
                raise Exception(u'Ya existe un jugador con ese nombre de usuario.')
            else:
                super(Player, self).save(*args, **kwargs)
        else:
            super(Player, self).save(*args, **kwargs)

